<?php
/**
 * 后台 ActiveField
 * 继承了\yii\bootstrap\ActiveField 添加了一些额外的东西
 */

namespace common\widgets;

use yii\helpers\Html;
use yii\helpers\ArrayHelper;

class ActiveField extends \yii\bootstrap\ActiveField {
    
    /**
     * 插件配置
     * @var array
     */
    public $pluginOptions = [];

    /**
     * 重新封装 renderLabelParts
     * @param string|null $label the label or null to use model label
     * @param array $options the tag options
     */
    protected function renderLabelParts($label = null, $options = [])
    {
        $options = array_merge($this->labelOptions, $options);
        if ($label === null) {
            if (isset($options['label'])) {
                $label = $options['label'];
                unset($options['label']);
            } else {
                $attribute = Html::getAttributeName($this->attribute);
                $label = Html::encode($this->model->getAttributeLabel($attribute));
            }
        }
        if (!isset($options['for'])) {
            $options['for'] = Html::getInputId($this->model, $this->attribute);
        }
        $this->parts['{beginLabel}'] = Html::beginTag('label', $options);
        $this->parts['{endLabel}'] = Html::endTag('label');
        if (!isset($this->parts['{labelTitle}'])) {
            $this->parts['{labelTitle}'] = $label;
        }
    }


    /*
     * 重新封装hiddenInput
     * 原来的hiddenInput有label等其他无用元素，现将这些元素剔除，只保留一个input
     */
    public function hiddenInput($options = [])
    {
        $this->parts['{label}'] = "";
        $this->parts['{hint}'] = "";
        $this->parts['{error}'] = "";
        $this->parts['{beginWrapper}'] = "";
        $this->parts['{endWrapper}'] = "";
        $this->parts['{input}'] = Html::activeHiddenInput($this->model, $this->attribute, $options);
        return $this;
    }


    /*
     * 即使搜索下拉插件
     * @应用场合：比如下拉选择大量数据的时候，用这个最好
     * @原理：将真实的input隐藏，然后添加一个input，利用typeahead.js 插件达到效果，再将选择的值传回给 隐藏的input的value
     * @param string $url ajax获取的地址
     */
    public function suggestInput($url, $options = [])
    {
        //获取ID并分配ID
        if (!array_key_exists('id', $options)) {
            $options['id'] = Html::getInputId($this->model, $this->attribute);
        }
        $id = $options['id'];
        $options['id'] = $options['id'].'-suggest';

        //JS代码部分
        $View = $this->form->getView();
        $View->registerJsFile('@web/assets/plugins/bootstrap-typeahead.js', ['depends'=>\yii\web\JqueryAsset::className()]);
        $js = "
            function displayResult(item, val, text) {
                jQuery('#". $id . "').val(val);
            }
            jQuery('#" . $options['id'] . "').typeahead({
                ajax: {
                    url: '" . $url . "',
                    timeout: 1000,
                    triggerLength: 1
                },
                itemSelected: displayResult
            });
            ";

        $View->registerJs($js);

        $options = array_merge($this->inputOptions, $options);
        $this->adjustLabelFor($options);
        $hidden = Html::activeHiddenInput($this->model, $this->attribute, ['id'=>$id]);
        $input  = Html::input('text', '', '', $options);
        $this->parts['{input}'] = $hidden . $input;

        return $this;
    }

}